<template>
	<view>
		<view class="goods-list">
			<view v-for="(item,index) in goodsList" :key="index" @click="gotoDeatil(item)">
				<my-goods :goods="item"></my-goods>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				queryObj:{
					query:'',
					cid:'',
					pagenum:1,
					pageList:18
				},
				//商品列表的数据
				goodsList:[],
				//总数据
				total:0,
				// defaultPic:'https://img.tukuppt.com/png_preview/00/04/81/SYZxWQlAr9.jpg!/fw/780'
				//
				isloading: false
			};
		},
		onLoad(options){
			this.queryObj.query = options.query || ''
			this.queryObj.cid = options.cid || ''
			this.getGoodsList()
		},
		methods: {
			async getGoodsList(stop){
				//打开节流阀
				this.isloading = true
				const { data: res } = await uni.$http.get('/api/public/v1/goods/search',this.queryObj)
				//关闭节流阀
				this.isloading = false
				//只要数据请求完毕就立即调用stop回调函数
				stop && stop()
				if(res.meta.status !== 200) return uni.$showMsg()
				//为数据进行赋值
				this.goodsList = [...this.goodsList,...res.message.goods]
				this.total = res.message.total
			},gotoDeatil(item){
			uni.navigateTo({
				url:`/subpkg/goods_detail/goods_detail?goods_id=${item.goods_id}`
			})
		}
		},
		
		onReachBottom(){
			if(this.queryObj.pagenum * this.queryObj.pageList >= this.total) return uni.$showMsg('数据加载完毕')
			//判断是否正在请求他数据
			if(this.isloading) return
			this.queryObj.pagenum += 1
			this.getGoodsList()
		},
		//下拉刷新事件
		onPullDownRefresh(){
			//重置关键数据
			this.queryObj.pagenum = 1
			this.total = 0
			this.isloading = false
			this.goodsList = []
			//重新发起请求
			this.getGoodsList(() => uni.stopPullDownRefresh() )
		}
	}
</script>

<style lang="scss">
.goods-item{
	display: flex;
	padding: 10px 5px;
	border-bottom: 1px solid #f0f0f0;
	.goods-item-left{
		margin-right: 5px;
		.goods-pic{
			width: 100px;
			height: 100px;
		}
	}
	.goods-item-right{
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		.goods-name{
			font-size: 12px;
			overflow: hidden;
			text-overflow: ellipsis;
			display: -webkit-box;
			-webkit-box-orient: vertical;
			-webkit-line-clamp: 2;
		}
		.goods-price{
			font-size: 16px;
			color: #c00000;
		}
	}
}
</style>
